package com.woniu.function.service.impl;

import com.woniu.entity.Result;
import com.woniu.function.entity.DTO.RescueDTO;
import com.woniu.function.service.IFunctionService;
import com.woniu.order.pojo.Order;
import com.woniu.pay.entity.PayObject;
import com.woniu.pay.feign.AlipayFeignClient;
import com.woniu.shop.feign.StoreServiceFeignClient;
import com.woniu.shop.pojo.StoreService;
import com.woniu.user.feign.UserFeignClient;
import com.woniu.user.pojo.User;
import com.woniu.util.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class FunctionServiceImpl implements IFunctionService {

    @Autowired
    private UserFeignClient userFeignClient;

    @Autowired
    private StoreServiceFeignClient storeServiceFeignClient;

    @Autowired
    private AlipayFeignClient alipayFeignClient;

    @Override
    public Result rescue(RescueDTO rescueDTO) {
        int i = 1;
        User user = userFeignClient.getUserByPhone(rescueDTO.getPhone());
        StoreService serviceData = storeServiceFeignClient.getService(rescueDTO.getServiceType());

        IdWorker idWorker = new IdWorker(0,1);
        long orderNo = idWorker.nextId();
        Order order = new Order();
        order.setShopId(serviceData.getShopId());
        order.setUserId(user.getId());
        order.setUserName(user.getName());
        order.setPayType(1);
        order.setNo(String.valueOf(orderNo));
        order.setPayAmount(serviceData.getServiceAmount());
        PayObject payObject = new PayObject();
        payObject.setOutTradeNo(order.getNo());
        payObject.setTotalAmount(String.valueOf(serviceData.getServiceAmount()));
//        Result result = alipayFeignClient.toPay(payObject);
        System.out.println("救援的服务是:"+serviceData.getServiceName());
        System.out.println();
        System.out.println("救援人："+user.getName()+"===手机号："+user.getPhone());
        System.out.println();
        System.out.println("救援地址："+rescueDTO.getIp());
        return Result.ok(200,"");
    }

    @Override
    public Result oil(String phone, String storeServiceId) {
        User user = userFeignClient.getUserByPhone(phone);

        StoreService serviceData = storeServiceFeignClient.getService(storeServiceId);


        IdWorker idWorker = new IdWorker(0,1);
        long orderNo = idWorker.nextId();
        Order order = new Order();
        order.setShopId(serviceData.getShopId());
        order.setUserId(user.getId());
        order.setUserName(user.getName());
        order.setPayType(1);
        order.setNo(String.valueOf(orderNo));
        order.setPayAmount(serviceData.getServiceAmount());
        PayObject payObject = new PayObject();
        payObject.setOutTradeNo(order.getNo());
        payObject.setTotalAmount(String.valueOf(serviceData.getServiceAmount()));
//        Result result = alipayFeignClient.toPay(payObject);
        System.out.println("加油成功");
        return Result.ok(200,"");
    }
}
